package org.gdufe.comment.service.impl;

import org.gdufe.comment.feignclient.BookServiceFeignClient;
import org.gdufe.comment.service.CommentService;
import org.gdufe.comment.service.CountPointScheduleService;
import org.gdufe.pojos.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CountPointSechduleServiceImpl implements CountPointScheduleService {
    @Autowired
    private CommentService commentService;
    @Autowired
    private BookServiceFeignClient bookServiceFeignClient;

    //定时任务，每天凌晨4点执行
    @Scheduled(cron = "* * 4 1/1 * ? ")
    public void countPoint(){
        //远程调用bookservice获取当前所有书籍的id集合
        R r1 = bookServiceFeignClient.listId();
        List<Integer> idList= (List<Integer>) r1.getData();

        for (Integer id:idList) {
            R r = commentService.querryPoint(id);
            Double point = (Double)r.getData();

        //远程调用bookservice更新point
            R r2 = bookServiceFeignClient.updatePoint(id, point);
            System.out.println("更新结果："+r2);
        }
    }
}
